package com.example.myservice_provider.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myservice_provider.Mapper.CommentMapper;
import com.example.myservice_provider.Mapper.PostMapper;
import com.example.myservice_provider.entity.Comment;
import com.example.myservice_provider.entity.Post;
import com.example.myservice_provider.entity.Users;
import com.example.myservice_provider.service.PostService;
import com.example.myservice_provider.service.UserService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserService userService;

    @Override
    public Page<Post> getAllPosts(Integer page, String classification, Integer PosterId) throws Exception {
        LambdaQueryWrapper<Post> lqw = new LambdaQueryWrapper<Post>();
        if(!classification.isEmpty()){
            lqw.eq(Post::getClassification,classification);
        }
        if(PosterId!=null){
            lqw.eq(Post::getPosterID,PosterId);
        }
        lqw.orderByDesc(Post::getPostID);
        //page设置
        Page<Post> postPage = new Page<>();
        postPage.setCurrent(page);
        postPage.setSize(20);

        System.out.println(classification+"==="+PosterId);
        Page<Post> postPage1 = this.postMapper.selectPage(postPage, lqw);

        List<Post> records = postPage1.getRecords();
        //关联用户
//        System.out.println(records);
        for (Post record : records) {
            Users userInfo = this.userService.getUserInfo(record.getPosterID());
            record.setPostername(userInfo.getUsername());
            record.setPosterimg(userInfo.getAvatar());
        }

        postPage1.setRecords(records);


        return postPage1;
    }

    @Override
    public Post getOnePost(Integer postId) throws Exception {
        Post post = this.postMapper.selectById(postId);

        Users userInfo = this.userService.getUserInfo(post.getPosterID());
        post.setPostername(userInfo.getUsername());
        post.setPosterimg(userInfo.getAvatar());
        return post;
    }

    @Override
    public int starPost(Integer postId) throws Exception {
        Post post = this.postMapper.selectById(postId);
        post.setStars(post.getStars()+1);
        return this.postMapper.updateById(post);
    }

    @Override
    public int updatePost(Post post) throws Exception {
        post.setUpdateTime(LocalDateTime.now());
        return this.postMapper.updateById(post);
    }

    @Override
    public int addPost(Post post) throws Exception {
        //该用户帖子数加一
        Users userInfo = this.userService.getUserInfo(post.getPosterID());
        userInfo.setPostNum(userInfo.getPostNum()+1);
        this.userService.updateUser(userInfo);
        //新建帖子
        post.setCreateTime(LocalDateTime.now());
        return this.postMapper.insert(post);
    }


    //评论模块
    @Override
    public int addCommon(Comment comment) throws Exception {
        comment.setComTime(LocalDateTime.now());
        return this.commentMapper.insert(comment);
    }

    @Override
    public Page<Comment> getCommon(Integer page, Integer postID) throws Exception {
        LambdaQueryWrapper<Comment> lqw = new LambdaQueryWrapper<Comment>();

        lqw.eq(Comment::getPostID,postID);

        //page设置
        Page<Comment> commentPage = new Page<>();
        commentPage.setCurrent(page);
        commentPage.setSize(20);

        Page<Comment> commentPage1 = this.commentMapper.selectPage(commentPage, lqw);

        List<Comment> records = commentPage1.getRecords();

        for (Comment record : records) {
            Users userInfo = this.userService.getUserInfo(record.getUserID());
            record.setUsername(userInfo.getUsername());
            record.setUserimg(userInfo.getAvatar());
        }

        commentPage1.setRecords(records);
        return commentPage1;
    }

    @Override
    public int starCommon(Integer commentID) throws Exception {
        Comment comment = this.commentMapper.selectById(commentID);
        comment.setStar(comment.getStar()+1);
        return this.commentMapper.updateById(comment);
    }
}
